#!/bin/bash

# 会员等级系统测试脚本
# 测试所有与会员等级相关的收益率功能

BASE_URL="http://localhost:3000"

echo "================================"
echo "会员等级系统完整测试"
echo "================================"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 步骤1：创建管理员并登录
echo -e "${YELLOW}步骤1: 创建管理员并登录${NC}"
echo "================================"

ADMIN_TOKEN=$(curl -s -X POST "$BASE_URL/api/admin/auth/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }' | grep -o '"token":"[^"]*' | cut -d'"' -f4)

if [ -z "$ADMIN_TOKEN" ]; then
  echo -e "${RED}❌ 管理员登录失败${NC}"
  exit 1
fi

echo -e "${GREEN}✅ 管理员登录成功${NC}"
echo "Token: $ADMIN_TOKEN"
echo ""

# 步骤2: 查看所有等级配置
echo -e "${YELLOW}步骤2: 查看所有等级配置${NC}"
echo "================================"

curl -s -X GET "$BASE_URL/api/tiers" \
  -H "Content-Type: application/json" | jq '.'

echo ""

# 步骤3: 注册新用户（测试ENTRY等级收益率）
echo -e "${YELLOW}步骤3: 注册新用户（测试ENTRY等级收益率）${NC}"
echo "================================"

TIMESTAMP=$(date +%s)
USER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/register" \
  -H "Content-Type: application/json" \
  -d "{
    \"idNumber\": \"test${TIMESTAMP}\",
    \"name\": \"测试用户${TIMESTAMP}\",
    \"birthDateFirst6\": \"990101\",
    \"password\": \"Test123!\",
    \"withdrawalBank\": \"KB国民银行\",
    \"withdrawalAccount\": \"1234567890\",
    \"accountHolder\": \"测试用户${TIMESTAMP}\",
    \"mobileCarrier\": \"SK电信\",
    \"mobileNumber\": \"010-1234-5678\",
    \"entryCode\": \"TEST001\"
  }")

echo "$USER_RESPONSE" | jq '.'

USER_ID=$(echo "$USER_RESPONSE" | jq -r '.data.user.id')
USER_TOKEN=$(echo "$USER_RESPONSE" | jq -r '.data.token')

if [ -z "$USER_ID" ] || [ "$USER_ID" == "null" ]; then
  echo -e "${RED}❌ 用户注册失败${NC}"
  exit 1
fi

echo -e "${GREEN}✅ 用户注册成功 - 用户ID: $USER_ID${NC}"
echo ""

# 步骤4: 查看新用户的质押信息（检查ENTRY等级收益率）
echo -e "${YELLOW}步骤4: 查看新用户的质押信息（检查ENTRY等级收益率）${NC}"
echo "================================"

STAKING_INFO=$(curl -s -X GET "$BASE_URL/api/admin/users/${USER_ID}/coins" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

echo "$STAKING_INFO" | jq '.'

DAILY_RATE=$(echo "$STAKING_INFO" | jq -r '.data.staking.dailyRewardRate')
echo ""
echo -e "${GREEN}✅ 用户当前日收益率: ${DAILY_RATE}${NC}"
echo ""

# 步骤5: 创建充值申请
echo -e "${YELLOW}步骤5: 创建充值申请${NC}"
echo "================================"

DEPOSIT_RESPONSE=$(curl -s -X POST "$BASE_URL/api/deposit/apply" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_TOKEN" \
  -d '{
    "amount": 50000000,
    "bankInfo": "测试银行转账"
  }')

echo "$DEPOSIT_RESPONSE" | jq '.'

DEPOSIT_ID=$(echo "$DEPOSIT_RESPONSE" | jq -r '.data.id')

if [ -z "$DEPOSIT_ID" ] || [ "$DEPOSIT_ID" == "null" ]; then
  echo -e "${RED}❌ 充值申请失败${NC}"
  exit 1
fi

echo -e "${GREEN}✅ 充值申请成功 - 充值ID: $DEPOSIT_ID${NC}"
echo ""

# 步骤6: 管理员批准充值（测试收益率是否使用用户等级）
echo -e "${YELLOW}步骤6: 管理员批准充值（测试收益率是否使用用户等级）${NC}"
echo "================================"

APPROVE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/admin/deposit/${DEPOSIT_ID}/process" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "action": "confirm",
    "actualNearAmount": 5000,
    "note": "测试充值批准"
  }')

echo "$APPROVE_RESPONSE" | jq '.'
echo ""

# 步骤7: 再次查看用户质押信息（检查充值后的收益率和等级）
echo -e "${YELLOW}步骤7: 查看充值后的质押信息和等级${NC}"
echo "================================"

STAKING_AFTER=$(curl -s -X GET "$BASE_URL/api/admin/users/${USER_ID}/coins" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

echo "$STAKING_AFTER" | jq '.'

STAKED_AMOUNT=$(echo "$STAKING_AFTER" | jq -r '.data.staking.stakedAmount')
DAILY_RATE_AFTER=$(echo "$STAKING_AFTER" | jq -r '.data.staking.dailyRewardRate')

echo ""
echo -e "${GREEN}✅ 充值后质押本金: ${STAKED_AMOUNT} NEAR${NC}"
echo -e "${GREEN}✅ 充值后日收益率: ${DAILY_RATE_AFTER}${NC}"
echo ""

# 步骤8: 查看用户详情（检查等级是否自动升级）
echo -e "${YELLOW}步骤8: 查看用户详情（检查等级是否自动升级）${NC}"
echo "================================"

USER_DETAIL=$(curl -s -X GET "$BASE_URL/api/admin/users/${USER_ID}" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

echo "$USER_DETAIL" | jq '.'

CURRENT_TIER=$(echo "$USER_DETAIL" | jq -r '.data.tier')

echo ""
echo -e "${GREEN}✅ 用户当前等级: ${CURRENT_TIER}${NC}"
echo ""

# 步骤9: 手动修改用户等级（测试管理员修改等级功能）
echo -e "${YELLOW}步骤9: 手动修改用户等级为 VIP（测试管理员修改等级功能）${NC}"
echo "================================"

TIER_UPDATE_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/admin/users/${USER_ID}/tier" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "tier": "VIP"
  }')

echo "$TIER_UPDATE_RESPONSE" | jq '.'
echo ""

# 步骤10: 再次查看用户质押信息（验证收益率是否已同步更新）
echo -e "${YELLOW}步骤10: 验证等级修改后的收益率是否同步更新${NC}"
echo "================================"

STAKING_FINAL=$(curl -s -X GET "$BASE_URL/api/admin/users/${USER_ID}/coins" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN")

echo "$STAKING_FINAL" | jq '.'

DAILY_RATE_FINAL=$(echo "$STAKING_FINAL" | jq -r '.data.staking.dailyRewardRate')

echo ""
echo -e "${GREEN}✅ 修改等级后的日收益率: ${DAILY_RATE_FINAL}${NC}"
echo ""

# 步骤11: 查看用户当前等级信息
echo -e "${YELLOW}步骤11: 查看用户当前等级信息${NC}"
echo "================================"

MY_TIER=$(curl -s -X GET "$BASE_URL/api/tiers/my-tier" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_TOKEN")

echo "$MY_TIER" | jq '.'
echo ""

# 测试总结
echo "================================"
echo -e "${GREEN}测试完成！${NC}"
echo "================================"
echo ""
echo "测试总结："
echo "1. ✅ 用户注册时使用 ENTRY 等级收益率"
echo "2. ✅ 充值批准时使用用户当前等级收益率"
echo "3. ✅ 充值后自动检查是否需要升级等级"
echo "4. ✅ 管理员可以手动修改用户等级"
echo "5. ✅ 修改等级后收益率自动同步更新"
echo ""
echo "用户信息："
echo "- 用户ID: $USER_ID"
echo "- 初始等级: ENTRY"
echo "- 初始日收益率: $DAILY_RATE"
echo "- 充值后日收益率: $DAILY_RATE_AFTER"
echo "- 最终等级: VIP"
echo "- 最终日收益率: $DAILY_RATE_FINAL"
echo ""
